﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaskelot.SP.Model.Client;
using Kaskelot.SP.Model.Client.Forms;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;

namespace Kaskelot.SP.Services.Client.Mapping
{
    public static class ClientFaltExtensions
    {
        public static ClientFalt ToClientContract(this Falt entity)
        {
            ClientFalt f = new ClientFalt();

            CopyFromEntity(entity, f, false);

            return f;
        }

        public static ClientFalt ToMinimalClientContract(this Falt entity)
        {
            ClientFalt f = new ClientFalt();

            CopyFromEntity(entity, f, true);

            return f;
        }

        public static Falt ToContract(this ClientFalt f)
        {
            Falt entity = new Falt();

            CopyToEntity(f, entity);

            return entity;
        }

        public static Falt UpdateContract(this Falt falt, FaltForm form)
        {
            if (falt.Id == Guid.Empty)
            {
                falt.Id = Guid.NewGuid();
            }

            falt.Namn = form.Namn;
            falt.Default = form.Default;
            falt.Typ = (FaltTyp)form.Typ;
            falt.Required = form.Required;

            return falt;
        }

        private static void CopyFromEntity(Falt from, ClientFalt to, bool minimal)
        {
            to.Id = from.Id;
            to.ValId = from.ValId;
            to.Namn = from.Namn;
            to.Default = from.Default;
            to.Typ = from.Typ.ToString();
            to.Required = from.Required;
        }

        private static void CopyToEntity(ClientFalt from, Falt to)
        {
            to.Id = from.Id;
            to.ValId = from.ValId;
            to.Namn = from.Namn;
            to.Default = from.Default;
            to.Typ = (FaltTyp)Enum.Parse(typeof(FaltTyp), from.Typ);
            to.Required = from.Required;
        }
    }
}
